/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import koneksi.Koneksi;

/**
 *
 * @author Dante
 */
public class MahasiswaDAO {

    Koneksi k = new Koneksi();
    Connection con = null;

    public MahasiswaDAO(){
        con = k.getConnect();
    }

    public String Insert(Mahasiswa m){
        String status = "Gagal";
        try {
            PreparedStatement ps = con.prepareStatement("INSERT INTO tmahasiswa VALUES (?,?,?,?);");
            ps.setString(1, m.getNim());
            ps.setString(2, m.getNama());
            ps.setString(3, m.getAlamat());
            ps.setString(4, m.getFakultas());
            ps.executeUpdate();
            status = "Insert Sukses";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return status;
    }

    public String Delete(String nim){
        String status = "Gagal";
        try {
            PreparedStatement ps = con.prepareStatement("DELETE FROM tmahasiswa WHERE nim = ?");
            ps.setString(1, nim);
            ps.executeUpdate();
            status = "Delete Sukses";
        } catch (Exception e) {
        }
        return status;
    }

    public String Update(String nim,Mahasiswa m){
        String status = "Gagal";
        try {
            PreparedStatement ps = con.prepareStatement("UPDATE tmahasiswa SET nama=?,alamat=?,fakultas=? WHERE nim=?");
            ps.setString(1, m.getNama());
            ps.setString(2, m.getAlamat());
            ps.setString(3, m.getFakultas());
            ps.setString(4, nim);
            ps.executeUpdate();
            status = "Sukses Update";
        } catch (Exception e) {
        }
        return status;
    }

    public List<Mahasiswa> getMahasiswa(){
        List<Mahasiswa> list = new ArrayList<Mahasiswa>();
        try {
            PreparedStatement ps = con.prepareStatement("SELECT * FROM tmahasiswa");
            ResultSet rs  = ps.executeQuery();
            while(rs.next()){
                Mahasiswa mhs = new Mahasiswa();
                mhs.setNim(rs.getString("nim"));
                mhs.setNama(rs.getString("nama"));
                mhs.setAlamat(rs.getString("alamat"));
                mhs.setFakultas(rs.getString("fakultas"));
                list.add(mhs);
            }
        } catch (Exception e) {
        }
        return list;
    }

//    public static void main(String[] args) {
//        MahasiswaDAO dao = new MahasiswaDAO();
//        Mahasiswa m = new Mahasiswa();
//        Insert
//        m.setNim("672009000");
//        m.setNama("tes");
//        m.setAlamat("Salatiga");
//        m.setFakultas("FTI UKSW");
//        System.out.println(dao.Insert(m));

//        Delete
//        System.out.println(dao.Delete("672009000"));

//        Update
//        m.setNama("Adhe Wahyu Ardanto");
//        m.setAlamat("Kab. Semarang");
//        m.setFakultas("FTI UKSW");
//        System.out.println(dao.Update("672009055", m));

//        View
//        for (int i = 0; i < dao.getMahasiswa().size(); i++) {
//            System.out.println("NIM : "+dao.getMahasiswa().get(i).getNim());
//            System.out.println("Nama : "+dao.getMahasiswa().get(i).getNama());
//            System.out.println("Alamat : "+dao.getMahasiswa().get(i).getAlamat());
//            System.out.println("Fakultas : "+dao.getMahasiswa().get(i).getFakultas());
//
//        }
//    }

}
